<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: swc
 * @Date: 2024-01-23 11:03:46
 * @LastEditors: swc shanwenchao12138@163.com
 * @LastEditTime: 2025-03-19 17:12:03
-->
<!--
******功能应用组件模板******
功能应用组件:是最终应用程序的根组件,它注册在系统功能里。例如：权限设置，开发模块，逻辑引擎等
引用注册在全局Tsrv.compBox里, 参数需要compID+name+funName写全。
参考开发模块devModal写法
-->

<template>
  <div class="init-container">
    <o-base-grid
      :config="_config"
      :tableType="'refbasegrid'"
      :compBoxInfo="GT_compBoxInfo"
    />
  </div>
</template>

<script lang="ts">
import { N_oSetupPanel } from '../o-ref-comp/config';
import { I_cmd, T_cmd, T_srv, Tsrv, ifs, vue } from './config';

@T_srv.comp
export default class oRefBaseGrid extends vue {
  /* 父组件compBoxInfo */
  @T_srv.prop({
    required: false,
    type: Object,
    default: null
  })
  compBoxInfo: ifs.I_compBoxInfo;

  @T_srv.prop({
    required: true,
    type: Object,
    default: null
  })
  config: any;

  /* 组件名 */
  private name: string = 'oRefBaseGrid';

  private funName: string = '';

  private G_compName: string = this.name;

  /* 组件箱信息对象 */
  private GT_compBoxInfo: ifs.I_compBoxInfo;

  /* 组件箱对象 */
  private GT_compBox: ifs.I_compBox;

  /* 组件箱识别ID */
  private G_compID: string;

  /* 指挥部 */
  private GT_command: I_cmd = new T_cmd();

  /* 遮罩 */
  public isMask: boolean = false;

  private _config: any = null;

  // map <监听事件，源事件>
  private forward: Map<string, string> = new Map();

  private refTable: string = '';

  // 监听类型
  private listenType: string = 'default';

  created() {
    // 如果父组件没有传参，那就产生一个
    this.GT_compBoxInfo = Tsrv.getCompInfo(this.compBoxInfo);
    this.G_compID = this.GT_compBoxInfo.compID;
    this.GT_compBox = this.GT_compBoxInfo.TcompBox;
    this.GT_command.init(this, this.GT_compBoxInfo);
    this.init();
  }

  mounted() {
    /* 向父组件发送实例 */
    this.GT_compBox.addCompMergeName(this.G_compID, this.G_compName, this);
    this.$emit('compBoxInfo', this.GT_compBoxInfo);
    if (this._config) {
      // 配置监听listentype
      this.listenType = this._config['listenType'] || 'default';
    }
    this.receiveEvent();
  }

  destroyed() {
    /* 注册组件销毁 */
    this.GT_compBox.removeCompMergeName(this.G_compID, this.G_compName);

    /* 如果父组件传入的compBoxInfo则删除本文件产生的引用,反之是本组件产生的ID统一删除 */
    if (Tsrv.utils.isEmpty(this.compBoxInfo)) {
      this.GT_compBox.removeCompsWithCompID(this.G_compID);
    } else {
      this.GT_compBox.removeCompsMergeName(this.G_compID, [
        `'${this.G_compName}'`
      ]);
    }
    if (this.forward.size > 0) {
      this.forward.forEach((v, k) => {
        if (this.listenType === 'default') {
          this.$bus.$off(k);
        } else {
          this.$mbus.$off(k);
        }
      });
    }
  }

  private init() {
    this._config = Tsrv.utils.clone(this.config, true);
    this.refTable = this._config.refTable;
    this.GT_command.getFunColumnConf(this.refTable, this._config);
    this.funName = this.config.name; // 当前配置中的功能名
    const search = this._config.messageConfig?.emitActions?.search;
    if (!Tsrv.utils.noValue(search)) {
      search.forEach((item: any) => {
        const tID = Tsrv.utils.custUUID(16);
        this.forward.set(tID, item.toID);
        item.toID = tID;
      });
    }
  }

  /* 设置组件整体遮罩 */
  public setMask(p: boolean) {
    this.isMask = p;
  }

  public getMask(p: boolean): boolean {
    return this.isMask;
  }

  // 接收事件
  private receiveEvent() {
    if (this.forward.size > 0) {
      this.forward.forEach((forwardE, receE) => {
        if (this.listenType === 'default') {
          this.$bus.$on(receE, (m: any) => {
            Tsrv.reliablySendingBusEvent(
              forwardE,
              this.$bus,
              10,
              100,
              this.msgHandle(m)
            );
          });
        } else {
          this.$mbus.$on(receE, (m: any) => {
            Tsrv.reliablySendingBusEvent(
              forwardE,
              this.$mbus,
              10,
              100,
              this.msgHandle(m)
            );
          });
        }
      });
    }
  }

  msgHandle(m: any): N_oSetupPanel.N_comps.I_eventSearchMsg {
    const _m = Tsrv.utils.clone(
      m,
      true
    ) as N_oSetupPanel.N_comps.I_eventSearchMsg;
    _m.option.autoCompleteInfo.table = this.refTable;
    return _m;
  }
}
</script>
<style lang="scss" scoped></style>
